package com.neusoft.neuiotms.dcvm.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcvm.dto.MockDTO;
import com.neusoft.neuiotms.dcvm.model.Mock;
import com.neusoft.neuiotms.dcvm.repository.MockRepository;

@Component
public class MockConverter extends AbstractConverter<Mock, MockDTO> {

    @Autowired
    private MockRepository mockRepository;

    @Override
    public Mock toModel(MockDTO dto) {
        Mock model;
        if (dto.isNew()) {
            model = new Mock();
        } else {
            model = this.mockRepository.getOne(dto.getId());
        }
        model.setApi(dto.getApi());
        model.setData(dto.getData());
        model.setDescription(dto.getDescription());
        return model;
    }

    @Override
    public MockDTO toDTO(Mock model, boolean forListView) {
        final MockDTO dto = new MockDTO();
        dto.setId(model.getId());
        dto.setApi(model.getApi());
        dto.setData(model.getData());
        dto.setDescription(model.getDescription());
        return dto;
    }
}
